<template>
  <div v-loading="loading" class="app-container">
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item name="1">
        <template slot="title">
          <i class="el-icon-search" />搜索栏
        </template>
        <div class="head-container">
          <el-form class="searchform" :model="params" :inline="true" size="mini" label-width="auto">
            <el-form-item label="条码">
              <el-input v-model="params.upcNbrs" placeholder="多个条码以逗号分割" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="商品号">
              <el-input v-model="params.itemNbr" placeholder="输入条码检验码/商品号搜索" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="部门">
              <el-input v-model="params.deptNbr" placeholder="输入部门搜索" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="商品说明1">
              <el-input v-model="params.item1Desc" placeholder="输入商品说明1模糊匹配" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="供应商之商品代码">
              <el-input v-model="params.vendorStockId" placeholder="输入供应商之商品代码" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="供应商编号">
              <el-input v-model="params.vendorNbr" placeholder="输入供应商编号" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="供应商名称">
              <el-input v-model="params.vendorName" placeholder="输入供应商名称模糊匹配" clearable style="width: 200px;" size="mini" />
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="params.itemStatus" placeholder="选择搜索" clearable size="mini" style="width: 200px">
                <el-option :key="-102" label="OIF自动化出号失败" :value="-102" />
                <el-option :key="-103" label="HOST自动化rekey失败" :value="-103" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" style="float:right;margin-left:0px;" type="primary" icon="el-icon-refresh" round @click="resetForm">重置</el-button>
              <el-button size="mini" style="float:right;margin-left:0px;" type="primary" icon="el-icon-search" round @click="toQuery">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div style="padding-top:5px">
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-refresh" round @click="doRetry">Retry</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-circle-close" round @click="batchReject">批量驳回</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-download" round @click="doExportMacro">导出Macro</el-button>
      <el-table
        ref="itemTab"
        :data="data"
        :row-key="getRowKeys"
        stripe
        size="small"
        border
        style="width: 100%;"
        @header-dragend="colChange"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="条码" prop="upcNbr" :width="colWidths.upcNbr">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="toItemInfo(scope.row)">{{ scope.row.upcNbr }}</el-button>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="商品号" prop="itemNbr" :width="colWidths.itemNbr" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="部门" prop="deptNbr" :width="colWidths.deptNbr" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="商品说明1" prop="item1Desc" :width="colWidths.item1Desc" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="供应商之商品代码" prop="vendorStockId" :width="colWidths.vendorStockId" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="供应商编号" prop="vendorNbr" :width="colWidths.vendorNbr" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="供应商名称" prop="vendorName" :width="colWidths.vendorName" />
        <itemStatusForm />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="最近修改时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="toItemInfo(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page.sync="currentPage"
        :page-sizes="[10,50,100,500]"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <itemForm ref="itemForm" :parent="parent" />
  </div>
</template>

<script>
import itemForm from '@/views/new-item/info'
import initData from '@/utils/initData'
import { parseTime } from '@/utils/index'
import { Message } from 'element-ui'
import { automationRekey, batchRetryReject } from '@/api/new-item'
import { toItemInfo, doDeleteItem } from '@/utils/newItem'
import itemStatusForm from '@/views/part/itemStatus'
import { doDownload } from '@/api/download'

export default {
  components: { itemForm, itemStatusForm },
  mixins: [initData],
  data() {
    return {
      parent: this,
      ids: [],
      loading: false,
      activeName: '1'
    }
  },
  created() {
    this.$nextTick(() => {
    })
  },
  methods: {
    parseTime,
    toItemInfo,
    doDeleteItem,
    beforeInit() {
      this.url = '/new-item/automationRetryList'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    // 列表排序触发
    sortChange(val) {
      let sortType = ''
      if (val.order === 'descending') {
        sortType = ' DESC'
      } else if (val.order === 'ascending') {
        sortType = ' ASC'
      }
      this.params.sort = val.prop + sortType
      this.init()
    },
    // 查询
    toQuery() {
      this.page = 0
      this.currentPage = 1
      if (this.params.upcNbrs) {
        this.params.upcNbrs = this.params.upcNbrs.trim().replaceAll('，', ',')
      }
      this.init()
      this.doClearSelection()
    },
    // 查询栏重置触发
    resetForm() {
      this.params = {}
    },
    // 列表多选框触发
    handleSelectionChange(val) {
      this.ids = []
      val.forEach(item => {
        this.ids.push(item.id)
      })
    },
    // 翻页选择row的key值
    getRowKeys(row) {
      return row.id
    },
    // 清空选择项
    doClearSelection() {
      this.$refs.itemTab.clearSelection()
    },
    // 批量审核驳回
    batchReject() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要驳回的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$prompt('请输入驳回原因', '驳回 ' + this.ids.length + ' 条Item', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValidator: this.rejectRemarkValidator,
          type: 'warning'
        }).then((rejectRemark) => {
          this.loading = true
          batchRetryReject(this.ids, rejectRemark.value).then(res => {
            this.toQuery()
            Message({
              message: '审核完成。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    rejectRemarkValidator(val) {
      if (!val) {
        return '请填写驳回原因'
      } else if (val.length > 500) {
        return '驳回原因长度为1-500'
      }
      return true
    },
    // 批量导出
    doExportMacro() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要导出的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认导出选择的 ' + this.ids.length + ' 条item信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          doDownload('/new-item/exportMacro', { ids: this.ids }, 'new-item.xlsm').then(() => {
            this.toQuery()
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    // 批量提交重试
    doRetry() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要retry的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else if (this.ids.length > 200) {
        Message({
          message: '一次选择请少于200条。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认retry选择的 ' + this.ids.length + ' 条item信息?', '校验数据', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          automationRekey(this.ids).then(res => {
            Message({
              message: '提交成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.toQuery()
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    }
  }
}
</script>
